<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Settings extends CI_Controller {
    
    function __construct() {
        parent::__construct();
    }

    public function add_settings(){
        $setting_key = $this->input->post('start_time');
        $setting_val = $this->input->post('start_time_val');
        $data = $this->setting_model->add_settings($setting_key, $setting_val);
        $setting_key = $this->input->post('terms');
        $setting_val = $this->input->post('terms_val');
        $data = $this->setting_model->add_settings($setting_key, $setting_val);
        api_success($data);
    }
    
    public function set_setting() {
        $key = $this->input->post('setting_key', true);
        $val = $this->input->post('setting_val', true);
        $this->load->model('setting_model');
        $this->setting_model->set_setting($key, $val) ?
            api_success(array($key=>$val)) : api_failed('set_setting_failed');
    }

    public function set_settings(){
        $sets = $this->input->post(null, true);
        $this->load->model('setting_model');
        foreach ($sets as $set_key => $set_val) {
            $this->setting_model->set_setting($set_key, $set_val) ? 
            '' : api_failed("set_{$set_key}_failed");
        }
        api_success(1);
    }

    public function get_setting(){
        $setting_key = $this->input->get('setting_key', true);
        $this->load->model('setting_model');
        api_success(array(
            $setting_key => $this->setting_model->get_setting_val($setting_key)
        ));
    }
    
    public function get_all_settings() {
        $this->load->model('setting_model');
        $setting_rows = $this->setting_model->get_all_settings();
        $settings = array();
        foreach ($setting_rows as $setting_row) {
            $settings[$setting_row['setting_key']] = $setting_row['setting_val'];
        }
        api_success(array('settings' => $settings));
    }
}